package com.jumi.microservice.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.constant.refundgoods.CancelUserFlagEnum;
import com.jumi.microservice.dto.admin.refund.*;
import com.jumi.microservice.dto.excel.ExportExcelCommonResponse;
import com.jumi.microservice.dto.export.ExportAfterSealRequest;
import com.jumi.microservice.dto.export.ExportAfterSealResponse;
import com.jumi.microservice.service.admin.AdminRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author wangjingdao
 * @Date 2020-08-05 11:38
 */
@Api(tags = {"后台退款管理相关接口"})
@RestController
@RequestMapping("/admin/refund")

public class AdminRefundController extends BaseController {
    @Autowired
    AdminRefundService adminRefundService;

    @ApiOperation("退款添加--订单发起")
    @PostMapping("/addRefund")
    public ResponseResult<Boolean> addRefund(AdminRefundRequest adminRefundRequest) {
        adminRefundRequest.setOperatorId(getUid());
        adminRefundRequest.setOperatorName(getUserName());
        adminRefundRequest.setUserFlag(CancelUserFlagEnum.USER_FLAG_ADMIN.getCode().byteValue());
        return adminRefundService.addRefund(adminRefundRequest);
    }

    @ApiOperation("退款列表")
    @PostMapping("/getRefundList")
    public TableDataInfo<AdminRefundListResponse> getRefundList(AdminRefundListRequest request) {
        return adminRefundService.getRefundList(request);
    }

    @ApiOperation("退款详情")
    @PostMapping("/getRefundDetails")
    public ResponseResult<AdminRefundDetailsResponse> getRefundDetails(AdminRefundIdRequest request) {
        return adminRefundService.getRefundDetails(request);
    }

    @ApiOperation("退款原因对照字典")
    @GetMapping("/refundMoneyReason/dictionary")
    public ResponseResult<Map<Integer, String>> getRefundMoneyReason() {
        ResponseResult<Map<Integer, String>> result = new ResponseResult<>();
        result.setData(adminRefundService.getRefundMoneyReason());
        return result;
    }

    @ApiOperation("导出")
    @PostMapping("/exportAfterSealResponse")
    public void exportAfterSealResponse(ExportAfterSealRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportAfterSealResponse> exportOrderAllResponseExportExcelCommonResponse = adminRefundService.exportAfterSealResponse(request);
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportAfterSealResponse.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("退款添加--商品发起")
    @PostMapping("/refundGoods")
    public ResponseResult<Boolean> refundGoods(AdminRefundPriceRequest request) {
        request.setOperatorId(getUid());
        request.setOperatorName(getUserName());
        return adminRefundService.refundGoods(request);
    }

}
